package com.yili.oa.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yili.oa.entity.SearchBase;
import com.yili.oa.entity.TableBase;
import com.yili.oa.models.Payment;
import com.yili.oa.service.PaymentService;
import com.yili.oa.utils.Exception.AjaxException;
import com.yili.oa.utils.ListUtils;
import com.yili.oa.vo.request.PaymentReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lxr
 * @version v0.01
 * @date 2019/7/22 0022
 * @email 200890949@qq.com
 */
@Controller
@RequestMapping("payment")
public class PaymentApi {

    @Autowired
    private PaymentService paymentService;

    @GetMapping("/getList")
    @ResponseBody
    public TableBase getList(SearchBase search){
        PageHelper.startPage(search.getPage(),search.getLimit());
        List<Payment> list = new ArrayList<>();
        list = paymentService.getList(search);
        PageInfo pageInfo = new PageInfo<>(list,search.getLimit());
        TableBase tableBase = new TableBase();
        tableBase.setData(list);
        tableBase.setCode(0);
        tableBase.setMsg("");
        tableBase.setCount(pageInfo.getTotal());
        return tableBase;
    }

    @PostMapping("/add")
    @ResponseBody
    public String add(@RequestBody PaymentReq paymentReq){
        if (StringUtils.isBlank(paymentReq.getPaymentName())){
            throw new AjaxException("请输入付款渠道");
        }
        if(paymentReq.getPaymentId()==null){
            paymentService.add(paymentReq);
        }else {
            paymentService.update(paymentReq);
        }
        return "success";
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("/editButton/{paymentId}")
    public String addButton(@PathVariable("paymentId")String paymentId, ModelMap modelAndView){
        SearchBase searchBase = new SearchBase();
        searchBase.setId(paymentId);
        List<Payment> list =paymentService.getList(searchBase);
        Payment payment = new Payment();
        if (ListUtils.isNotNullOrEmpty(list)){
            payment=list.get(0);
        }
        modelAndView.addAttribute("info",payment);
        return "game/addPayment";
    }

    @RequestMapping("delete")
    @ResponseBody
    public Map<String,Object> deletePayment(@RequestBody List<Long> list){
        Map<String, Object> map = new HashMap<>();
        paymentService.delSignIn(list);
        map.put("result", "success");
        return map;
    }
}
